---@type string
local modid = 'yuemiheng'

local start_level = TUNING[string.upper('CONFIG_'..modid..'_startlv')]

AddPrefabPostInit("yuemiheng", function(inst)
    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent('ymh_kill_boss_getbuff')
    -- if start_level > 0 then
    --     if inst.components.ymh_lv then
    --         local val = inst.components.ymh_lv:_GetExpToLevel(start_level)
    --         inst.components.ymh_lv:GetExp(val+1)
    --     end
    -- end
end)

-- 罡拳（空手）初始伤害，每级提升伤害一次（类数函数增长），输出上不封顶（可在mod设置，设置上限则为1w）；
--  十级罡拳后带二级破坏（可以拆卸绝望石柱）；
AddComponentPostInit('combat',
---comment
---@param self component_combat
function(self)
    local old_GetAttacked = self.GetAttacked
    function self:GetAttacked(attacker,damage,weapon,stimuli,spdamage,...)

        local victim = self.inst

        if attacker and attacker.prefab and attacker.prefab == 'yuemiheng' and attacker.components.ymh_lv and weapon == nil and not self.inst._do_not_trigger_punch_amg_again then

            local lv = attacker.components.ymh_lv.lv_cur
            local bonus = 0
            if lv and lv > 0 then
                bonus = math.pow(lv,2)

                if TUNING[string.upper('CONFIG_'..modid..'_punchdmg')] then
                    bonus = math.min(bonus,10000)
                end

                if damage then
                    damage = damage + bonus
                end
            end


            local x, _, z = attacker:GetPosition():Get()
            local ents = TheSim:FindEntities(x,  0, z, 8, nil, {'player',"companion","wall","structure","abigail","glommer"})
            for _, v in pairs(ents) do
                if v.components.combat and YMHS:checkAlive(v) and not v.components.combat:IsAlly(attacker) then
                    local area_dmg = (damage or 0)+ (bonus or 0)
                    -- v.components.health:DoDelta(-area_dmg)
                    v._do_not_trigger_punch_amg_again = true
                    v.components.combat:GetAttacked(attacker,area_dmg)
                    v._do_not_trigger_punch_amg_again = nil
                    -- ---@type event_data_attacked
                    -- local event_data_attacked = { attacker = attacker, damage = area_dmg, damageresolved = area_dmg, original_damage = area_dmg}
                    -- v:PushEvent('attacked',event_data_attacked)
                end
            end

        end
        if TUNING[string.upper('CONFIG_'..modid..'_boss_buff')] then
            if attacker and attacker.prefab and attacker.prefab == 'yuemiheng' then
                -- 击杀巨兽boss后，永久增加20％的伤害，每只boss限制一次
                if attacker.components.ymh_kill_boss_getbuff then
                    local mult = attacker.components.ymh_kill_boss_getbuff:getBuff()
                    if mult > 0 then
                        damage = damage * (1+mult)
                    end
                end
            end
        end

        -- 角色能不能加上位面抵抗）
        if victim and victim.prefab and victim.prefab == 'yuemiheng' then
            if spdamage then
                spdamage.planar = (spdamage.planar or 0) * (1 - TUNING.MOD_YUEMIHENG.AVATAR.YUEMIHENG.PLANAR_ABSORB)
            end
        end
        return old_GetAttacked(self,attacker,damage,weapon,stimuli,spdamage,...)
    end
end)

